import type { TrainPlanType } from '../interface'

import { RoleEnum } from '@/role'
import { useObserver } from 'mobx-react'
import { stores } from '@/stores'
import { asserts, isEmpty } from '@/utils/share'

interface Props {
    item?: Partial<{
        center_check_status: TrainPlanType.CheckState
        center_check_time: string
        center_check_reason: string
        center_check_image: string

        province_check_status: TrainPlanType.CheckState
        province_check_time: string
        province_check_reason: string
        province_check_image: string

        city_check_status: TrainPlanType.CheckState
        city_check_time: string
        city_check_reason: string
        city_check_image: string

        is_check: TrainPlanType.CheckState
    }>
}

/** 审核的各种结果映射 */
export const stateMap = {
    [-1]: { text: '审核错误', color: 'error', color16: '#FF3141' },
    0: { text: '待审核', color: 'warning', color16: '#FF8F20' },
    1: { text: '通过', color: 'success', color16: '#00B578' },
    2: { text: '未通过', color: 'danger', color16: '#FF3141' },
}

/** 是不是{@link roleCheckMap}的key */
const isRoleCheckMapKey = (
    v: RoleEnum,
): v is RoleEnum.center | RoleEnum.province | RoleEnum.city => {
    return [RoleEnum.center, RoleEnum.province, RoleEnum.city].includes(v)
}

/** 不同角色的使用不同“审核”字段 */
const useRoleCheck = (props: Props) => {
    const errorCheckObj = {
        check: -1 as const,
        time: '-1',
        reason: '',
        image: '',
    }

    return useObserver(() => {
        if (isEmpty(props.item) || asserts.isUndefined(props.item)) {
            return {
                /** 审核状态 */
                checkState: errorCheckObj,
                /** 审核结果 */
                checkResult: stateMap['-1'],
            }
        }

        /** 不同角色，使用不同的审核状态字段 */
        const roleCheckMap = {
            [RoleEnum.center]: {
                check: props.item.center_check_status,
                time: props.item.center_check_time,
                reason: props.item.center_check_reason,
                image: props.item.center_check_image,
            },
            [RoleEnum.province]: {
                check: props.item.province_check_status,
                time: props.item.province_check_time,
                reason: props.item.province_check_reason,
                image: props.item.province_check_image,
            },
            [RoleEnum.city]: {
                check: props.item.city_check_status,
                time: props.item.city_check_time,
                reason: props.item.city_check_reason,
                image: props.item.city_check_image,
            },
        }

        /** 审核状态 */
        const checkState = (() => {
            const role_id = stores.userStore.getRoleInfo?.role_id
            if (!role_id) return errorCheckObj

            const res = isRoleCheckMapKey(role_id) ? roleCheckMap[role_id] : errorCheckObj
            return res
        })()

        /** 审核结果 */
        const checkResult = stateMap[checkState.check ?? -1]

        return {
            /** 审核状态 */
            checkState,
            /** 审核结果 */
            checkResult,
        }
    })
}

export default useRoleCheck
